package com.skd.mall.coupon.service.impl;

import com.skd.mall.coupon.dao.SeckillSkuRelationDao;
import com.skd.mall.coupon.entity.SeckillSkuRelationEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skd.common.utils.PageUtils;
import com.skd.common.utils.Query;

import com.skd.mall.coupon.dao.SeckillSessionDao;
import com.skd.mall.coupon.entity.SeckillSessionEntity;
import com.skd.mall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取开始时间
     * @return
     */
    private static String startDatetime(){
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 计算明天的日期时间
        LocalDateTime tomorrow = now.plusDays(0).withHour(0).withMinute(0).withSecond(0);
        // 创建日期时间格式器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化时间为字符串
        return tomorrow.format(formatter);
    }


    private static String endDatetime(){
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 计算日期时间
        LocalDateTime tomorrow = now.plusDays(2).withHour(23).withMinute(59).withSecond(59);
        // 创建日期时间格式器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化时间为字符串
        return tomorrow.format(formatter);
    }

    @Autowired
    private SeckillSkuRelationDao seckillSkuRelationDao;


    @Override
    public List<SeckillSessionEntity> getLater3DaysSession() {
        List<SeckillSessionEntity> sessionEntities = this.list(
                new QueryWrapper<SeckillSessionEntity>().between("start_time", startDatetime(), endDatetime())
        );
        List<SeckillSessionEntity> seckillSessionEntities = sessionEntities.stream().map(item -> {
            List<SeckillSkuRelationEntity> list = seckillSkuRelationDao.selectList(
                    new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", item.getId())
            );
            item.setRelationEntities(list);
            return item;
        }).collect(Collectors.toList());
        return seckillSessionEntities;
    }

    /*public static void main(String[] args) {
        String s = endDatetime();
        System.out.println("s = " + s);
        String s1 = startDatetime();
        System.out.println("s1 = " + s1);
    }*/

}